<template>
  <div>
    <el-form
      :inline="true"
      class="demo-form-inline"
      :model="formInline"
      label-width="70px"
      label-position="small"
    >
      <el-form-item label="账号">
        <el-input
          placeholder="输入账号模糊查询"
          v-model="formInline.username"
          clearable
          size="small"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select
          placeholder="请选择性别"
          v-model="formInline.sex"
          size="small"
          clearable
        >
          <el-option label="男" value="1"></el-option>
          <el-option label="女" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="真实名字" v-if="isScaling">
        <el-input
          placeholder="请输入真实名字"
          v-model="formInline.realname"
          clearable
          size="small"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="手机号码" v-if="isScaling">
        <el-input
          placeholder="请输入手机号码查询"
          v-model="formInline.phone"
          clearable
          size="small"
        ></el-input>
      </el-form-item>

      <el-form-item label="用户状态" v-if="isScaling">
        <el-select
          placeholder="请选择"
          v-model="formInline.status"
          size="small"
          clearable
        >
          <el-option label="请选择" value="choose"></el-option>
          <el-option label="正常" value="1"></el-option>
          <el-option label="冻结" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="searchFn()"
          >搜索</el-button
        >
        <el-button
          icon="el-icon-refresh-right"
          type="primary"
          size="small"
          @click="resetFn()"
          >重置</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-link :underline="false" @click="scalingFn()"
          ><span v-if="isScaling">收起</span><span v-if="!isScaling">展开</span
          ><i
            :class="
              isScaling
                ? 'el-icon-arrow-up el-icon--right'
                : 'el-icon-arrow-down el-icon--right'
            "
          ></i
        ></el-link>
      </el-form-item>
    </el-form>
    <el-form>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="adduser()"
        >
          添加用户
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-download"
          size="small"
          @click="derive()"
        >
          导出
        </el-button>
        <!-- <el-button type="primary" icon="el-icon-upload2" size="small">
          导入
        </el-button> -->
        <el-button
          type="primary"
          icon="el-icon-delete"
          size="small"
          @click="trashVisibleFn()"
        >
          回收站
        </el-button>
        <el-dropdown class="dropbtn" v-if="multipleSelection.length">
          <el-button size="small"
            >批量操作<i class="el-icon-arrow-down el-icon--right"></i
          ></el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>
              <el-popconfirm
                title="确定批量删除吗?"
                @confirm="userseleteBatchFn()"
                placement="top"
              >
                <span slot="reference"><i class="el-icon-delete"></i>删除</span>
              </el-popconfirm>
            </el-dropdown-item>
            <el-dropdown-item @click.native="usersfrozenBatchFn()"
              ><i class="el-icon-lock"></i>冻结</el-dropdown-item
            >
            <el-dropdown-item @click.native="usersjieFn()"
              ><i class="el-icon-unlock"></i>解冻</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
        <!-- <el-button type="primary" icon="el-icon-reading" size="small">
          高级查询
        </el-button> -->
      </el-form-item>
    </el-form>
    <!-- <div>
      <btns
        :isExport="true"
        :isAdduser="true"
        :isImport="true"
        :isBin="true"
        :isAdvanced="true"
        :dialog="dialog"
        :multipleTable="multipleSelection"
      ></btns>
    </div> -->

    <div>
      <tableList
        :headerData="headerData"
        :multipleSelection="multipleSelection"
        :tableData="tableData"
        :total="total"
        :num="multipleSelection.length"
        @SelectionChange="SelectionChange"
        @CurrentChange="CurrentChange"
        @SizeChange="SizeChange"
      >
        <template v-slot:avatar="{ row }">
          <div>
            <el-image
              class="headerimg"
              :src="row.avatar"
              :preview-src-list="[row.avatar]"
            ></el-image>
          </div>
        </template>
        <template v-slot:operationBtn="{ operationData }">
          <div>
            <el-button @click="changeFn(operationData)" type="text"
              >编辑</el-button
            >
            <el-divider
              direction="vertical"
              style="margin-left: 20px"
            ></el-divider>
            <el-dropdown>
              <span class="el-dropdown-link">
                更多<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item @click.native="readmoreFn(operationData)"
                  >详情</el-dropdown-item
                >
                <el-dropdown-item @click.native="openpwdFn(operationData)"
                  >密码</el-dropdown-item
                >
                <el-dropdown-item
                  ><el-popconfirm
                    placement="top"
                    title="确定删除?"
                    @confirm="userDelFn(operationData)"
                  >
                    <span slot="reference">删除</span>
                  </el-popconfirm>
                </el-dropdown-item>
                <el-dropdown-item>
                  <el-popconfirm
                    placement="top"
                    title="确定更改?"
                    @confirm="freezingFn(operationData)"
                  >
                    <span slot="reference">
                      {{ operationData.status == 1 ? "冻结" : "解冻" }}</span
                    >
                  </el-popconfirm>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </template>
      </tableList>
    </div>

    <!-- btns中的数据 -->
    <el-drawer
      :title="isedit ? (isreadmore ? '详情' : '编辑') : '新增'"
      :before-close="handleClose"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
      class="addscroll"
    >
      <div class="demo-drawer__content">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户账号" prop="username">
            <el-input
              v-model.trim="ruleForm.username"
              placeholder="请输入用户账号"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="登陆密码" prop="password" v-if="!isedit">
            <el-input
              size="small"
              v-model="ruleForm.password"
              placeholder="请输入登陆密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmpassword" v-if="!isedit">
            <el-input
              size="small"
              v-model="ruleForm.confirmpassword"
              placeholder="请重新输入登陆密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="用户姓名" prop="realname">
            <el-input
              size="small"
              v-model.trim="ruleForm.realname"
              placeholder="请输入用户姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="工号" prop="workNo">
            <el-input
              size="small"
              v-model.trim="ruleForm.workNo"
              placeholder="请输入工号"
            ></el-input>
          </el-form-item>
          <el-form-item label="职务" prop="post_dictText">
            <el-input
              size="small"
              v-model="ruleForm.post_dictText"
              placeholder="请选择职务"
              ><el-button
                slot="append"
                icon="el-icon-search"
                @click="dialogFormVisible = true"
                >选择</el-button
              ></el-input
            >
          </el-form-item>
          <el-form-item label="角色分配" prop="selectedroles">
            <el-select
              v-model="ruleForm.selectedroles"
              multiple
              placeholder="请选择"
              size="small"
              style="width: 100%"
              @change="selectedrolesFn()"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.roleName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="部门分配" prop="orgCodeTxt">
            <el-input
              size="small"
              v-model="ruleForm.orgCodeTxt"
              placeholder="点击选择部门"
              ><el-button
                slot="append"
                icon="el-icon-search"
                type="primary"
                @click="dialogVisible = true"
                >选择</el-button
              ></el-input
            >
          </el-form-item>
          <el-form-item label="租户分配" prop="tenantIdentity">
            <el-select
              v-model="ruleForm.tenantIdentity"
              multiple
              placeholder="请选择"
              size="small"
              style="width: 100%"
            >
              <el-option
                v-for="item in options1"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="身份" prop="relTenantIds">
            <el-radio-group v-model="ruleForm.relTenantIds">
              <el-radio :label="1">普通用户</el-radio>
              <el-radio :label="2">上级</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- <el-form-item label="头像" prop="avatar">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/jeecg-boot/sys/common/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item> -->

          <el-form-item label="头像" prop="avatar" label-width="113px">
            <el-upload
              class="avatar-uploader"
              action
              :show-file-list="false"
              :http-request="httprequest"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="生日" prop="birthday">
            <el-date-picker
              v-model="ruleForm.birthday"
              type="date"
              placeholder="选择日期"
              size="small"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="性别" prop="sex">
            <el-select
              v-model="ruleForm.sex"
              placeholder="请选择性别"
              size="small"
            >
              <el-option label="男" value="1"></el-option>
              <el-option label="女" value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              size="small"
              v-model.trim="ruleForm.email"
              placeholder="请输入邮箱"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="phone">
            <el-input
              size="small"
              v-model.trim="ruleForm.phone"
              placeholder="请输入手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item label="座机" prop="telephone">
            <el-input
              size="small"
              v-model.trim="ruleForm.telephone"
              placeholder="请输入座机"
            ></el-input>
          </el-form-item>
          <el-form-item label="工作流引擎" prop="activitiSync">
            <el-radio-group v-model="ruleForm.activitiSync">
              <el-radio :label="1">同步</el-radio>
              <el-radio :label="0">不同步</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- <el-divider></el-divider> -->
          <!-- <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item> -->
        </el-form>
        <div class="demo-drawer__footer" v-if="!isreadmore">
          <!-- <el-button @click="cancelForm">取 消</el-button>
          <el-button
            type="primary"
            @click="$refs.drawer.closeDrawer()"
            :loading="loading"
            >{{ loading ? "提交中 ..." : "确 定" }}</el-button
          > -->
          <el-popconfirm title="确定放弃编辑?" @confirm="resetForm()">
            <el-button slot="reference">取消</el-button>
          </el-popconfirm>
          <!-- <el-button @click="resetForm('ruleForm')">取消</el-button> -->
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
            class="tijiao"
            >确定</el-button
          >
        </div>
      </div>
    </el-drawer>

    <!-- 职务列表 -->
    <el-dialog
      title="职务选择"
      :visible.sync="dialogFormVisible"
      :before-close="handleClose"
      class="workchoose"
    >
      <div class="left">
        <el-form
          :inline="true"
          class="demo-form-inline"
          :model="Position"
          label-width="70px"
          label-position="small"
        >
          <el-form-item label="职务名称">
            <el-input
              placeholder="请输入职务名称"
              v-model="Position.username"
              clearablesearchFn
              size="small"
            >
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="small"
              @click="searchFn()"
              >搜索</el-button
            >
            <el-button
              icon="el-icon-refresh-right"
              type="primary"
              size="small"
              @click="resetFn()"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          :data="tableData2"
          style="width: 100%"
          @row-click="singleElection"
          highlight-current-row
        >
          <el-table-column align="center" width="50">
            <template slot-scope="scope">
              <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
              <el-radio
                class="radio"
                v-model="templateSelection"
                :label="scope.row.id"
                >{{ "" }}</el-radio
              >
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="职务名称"
            width="150"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="code"
            label="职务编码"
            width="150"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="postRank_dictText"
            label="职级"
            width="80"
          ></el-table-column>
        </el-table>
      </div>

      <div class="right">
        <div>已选职务</div>
        <el-table :data="tableData1" height="180" border style="width: 100%">
          <el-table-column prop="name" label="职务名称" width="120">
          </el-table-column>
          <el-table-column prop="operation" label="操作">
            <el-button @click="del(tableData1.id)" type="text">删除</el-button>
          </el-table-column>
        </el-table>
      </div>
      <div class="clear"></div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="chooseid(tableData1)"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog title="部门搜索" :visible.sync="dialogVisible" width="30%">
      <span>上级部门：</span>
      <el-tree
        :data="selectData"
        show-checkbox
        node-key="orgCodeTxt"
        :default-expanded-keys="[2, 3]"
        :props="defaultProps"
        @check="getkeys"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="choosedepart()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 重新输入密码 -->
    <el-dialog
      title="重新输入密码"
      :visible.sync="repwdVisible"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item
          label="用户账号"
          :label-width="formLabelWidth"
          prop="username"
          size="small"
        >
          <el-input v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="登陆密码"
          :label-width="formLabelWidth"
          prop="password"
          size="small"
        >
          <el-input
            v-model="ruleForm.password"
            autocomplete="off"
            show-password
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item
          label="确认密码"
          :label-width="formLabelWidth"
          prop="confirmpassword"
          size="small"
        >
          <el-input
            v-model="ruleForm.confirmpassword"
            autocomplete="off"
            clearable
            show-password
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelpwdFn()">关 闭</el-button>
        <el-button type="primary" @click="changepwdFn('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <trash :trashVisible="trashVisible" ref="trash"></trash>
  </div>
</template>

<script>
// import btns from "@/components/btns.vue";
// import bus from "../../utils/bus";
import { getDepartMent } from "@/api/departManagement";
import tableList from "@/components/tableList.vue";
import trash from "@/components/trash.vue";
import {
  searchlist,
  userdelete,
  userexportXls,
  sysDepartRolelist,
  rolequeryall,
  useradd,
  duplicatecheck,
  useredit,
  changePassword,
  userfrozenBatch,
  userImg,
  userdeleteBatch,
  tenantlist,
} from "@/api/login";
export default {
  name: "userManagement",
  components: {
    // btns,
    tableList,
    trash,
  },
  data() {
    var validateusername = (rule, value, callback) => {
      let _self = this;
      if (value == "") {
        callback(new Error("请输入用户账号"));
      } else {
        let data = {
          tableName: "sys_user",
          fieldName: "username",
          fieldVal: _self.ruleForm.username,
          dataId: _self.ruleForm.id,
        };
        duplicatecheck(data).then((res) => {
          // console.log(res);
          if (res.code == 500) {
            callback(new Error(res.message));
          } else {
            callback();
          }
        });
      }
    };
    var validatepassword = (rule, value, callback) => {
      let _self = this;
      let myreg =
        /^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$/;
      if (value == "") {
        callback(new Error("请输入登陆密码"));
      } else if (!myreg.test(value)) {
        callback(
          new Error("密码至少八个字符，至少一个字母，一个数字和一个特殊字符")
        );
      } else {
        callback();
      }
    };
    var validateconfirmpassword = (rule, value, callback) => {
      let _self = this;
      if (value == "") {
        callback(new Error("请重新输入登陆密码"));
      } else if (value != _self.ruleForm.password) {
        callback(new Error("两次密码不一致"));
      } else {
        callback();
      }
    };
    var validateemail = (rule, value, callback) => {
      let _self = this;
      let myreg =
        /^[\da-z]+([\-\.\_]?[\da-z]+)*@[\da-z]+([\-\.]?[\da-z]+)*(\.[a-z]{2,})+$/;
      if (value == "" || value == null) {
        callback();
      } else if (!myreg.test(value)) {
        callback(new Error("请输入正确格式的邮箱"));
      } else {
        callback();
      }
    };
    var validatephone = (rule, value, callback) => {
      let _self = this;
      let myreg =
        /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if (value == "" || value == null) {
        callback();
      } else if (!myreg.test(value)) {
        callback(new Error("请输入正确格式的手机号"));
      } else {
        callback();
      }
    };
    var validatetelephone = (rule, value, callback) => {
      let _self = this;
      let myreg = /0\d{2,3}-\d{7,8}/;
      if (value == "" || value == null) {
        callback();
      } else if (!myreg.test(value)) {
        callback(new Error("请输入正确格式的座机号"));
      } else {
        callback();
      }
    };
    var validateworkNo = (rule, value, callback) => {
      let _self = this;
      if (value == "") {
        callback(new Error("请输入工号"));
      } else {
        let data = {
          tableName: "sys_user",
          fieldName: "work_no",
          fieldVal: _self.ruleForm.workNo,
          dataId: _self.ruleForm.id,
        };
        duplicatecheck(data).then((res) => {
          // console.log(res);
          if (res.code == 500) {
            callback(new Error(res.message));
          } else {
            callback();
          }
        });
      }
    };
    return {
      formLabelWidth: "120px",
      repwdVisible: false,
      isreadmore: false,
      issearch: false,
      isedit: false,
      //btns数据
      selectData: [],
      defaultProps: {
        children: "children",
        label: "departName",
      },
      tableData2: [],
      tableData1: [],
      Position: {
        username: "",
      },
      dialogFormVisible: false,
      dialogVisible: false,
      templateSelection: "",
      //   当前选择的行的数据
      checkList: [],
      brr: [],
      options: [],
      options1: [],
      value1: [],
      value1: "",
      imageUrl: "",
      table: false,
      loading: false,
      // tempruleForm: {},
      ruleForm: {
        id: "",
        username: "",
        password: "",
        confirmpassword: "",
        realname: "",
        workNo: "",
        orgCodeTxt: "",
        post_dictText: "",
        selecteddeparts: "",
        selectedroles: [],
        tenantIdentity: [],
        relTenantIds: "",
        avatar: null,
        birthday: "",
        sex: "",
        email: "",
        phone: "",
        telephone: "",
        activitiSync: "",
      },
      rules: {
        username: [
          { required: true, validator: validateusername, trigger: "blur" },
        ],
        password: [
          { required: true, validator: validatepassword, trigger: "blur" },
        ],
        confirmpassword: [
          {
            required: true,
            validator: validateconfirmpassword,
            trigger: "blur",
          },
        ],
        realname: [
          { required: true, message: "请输入用户姓名", trigger: "blur" },
        ],
        workNo: [
          { required: true, validator: validateworkNo, trigger: "blur" },
        ],
        email: [{ validator: validateemail, trigger: "blur" }],
        phone: [{ validator: validatephone, trigger: "blur" }],
        telephone: [{ validator: validatetelephone, trigger: "blur" }],
      },
      //btns数据
      arr: [],
      checkedKeys: [],
      trashVisible: false,
      dialog: false,
      multipleSelection: [],
      isScaling: true,
      total: 0,
      pagesize: 10,
      currentpage: 1,
      statevalue: true,
      isAdd: true,
      formInline: {
        username: "",
        sex: "",
        realname: "",
        phone: "",
        status: "",
      },
      tableData: [],
      headerData: [
        {
          prop: "username",
          label: "用户账号",
          width: "120",
          sortable: true,
        },
        {
          prop: "realname",
          label: "用户姓名",
          width: "120",
        },
        {
          prop: "avatar",
          label: "头像",
          width: "120",
          slot: true,
        },
        {
          prop: "sex_dictText",
          label: "性别",
          width: "80",
          sortable: true,
        },
        {
          prop: "birthday",
          label: "生日",
          width: "120",
        },
        {
          prop: "phone",
          label: "手机号码",
          width: "120",
        },
        {
          prop: "orgCodeTxt",
          label: "部门",
          width: "200",
        },
        {
          prop: "departIds_dictText",
          label: "负责部门",
          width: "200",
        },
        {
          prop: "status_dictText",
          label: "状态",
        },
      ],
    };
  },

  mounted() {
    let _self = this;
    _self.searchFn();
    //btns方法
    _self.rolequeryallFn();
    _self.getDepartMentMessage();
    _self.chooseorgCode();
    _self.tantqueryFn();
  },
  methods: {
    selectedrolesFn() {
      let _self = this;
      console.log(_self.options);
      console.log(_self.options1);
    },
    tantqueryFn() {
      let _self = this;
      let data = {
        pageNo: _self.currentpage,
        pageSize: _self.pagesize,
      };
      tenantlist(data).then((res) => {
        console.log(res);
        // alert(1);
        if (res.code == 0) {
          _self.options1 = res.result.records;
          console.log(_self.options);
        }
      });
    },
    //部门选择
    getkeys(a, b) {
      let _self = this;
      // console.log(a);
      // console.log(b);
      _self.checkedKeys = [];
      _self.ruleForm.orgCodeTxt = "";
      _self.checkedKeys = b.checkedNodes;

      console.log(_self.checkedKeys);
    },
    choosedepart() {
      let _self = this;
      _self.ruleForm.orgCodeTxt = "";
      _self.arr = [];
      for (let item of _self.checkedKeys) {
        _self.arr.push(item.departName);
      }
      for (let item of _self.checkedKeys) {
        _self.brr.push(item.id);
      }
      _self.ruleForm.orgCodeTxt = _self.arr.join();
      console.log(_self.brr);
      console.log(_self.ruleForm.orgCodeTxt);
      _self.dialogVisible = false;
    },
    //职务选择
    chooseid(tableData1) {
      let _self = this;
      console.log(tableData1);
      if (tableData1.length != 0) {
        _self.ruleForm.post_dictText = tableData1[0].name;
        _self.dialogFormVisible = false;
      } else {
        _self.ruleForm.post_dictText = "";
        _self.dialogFormVisible = false;
      }
    },
    //批量解冻
    usersjieFn() {
      let _self = this;
      let arrid = [];
      for (let item of _self.multipleSelection) {
        arrid.push(item.id);
      }
      let bb = arrid.toString() + ",";
      console.log(bb);
      let data = {
        ids: bb,
        status: 1,
      };
      userfrozenBatch(data).then((res) => {
        if (res.code == 200) {
          _self.searchFn();
        }
      });
    },
    //批量冻结
    usersfrozenBatchFn() {
      let _self = this;
      let arrid = [];
      for (let item of _self.multipleSelection) {
        arrid.push(item.id);
      }
      let bb = arrid.toString() + ",";
      console.log(bb);
      let data = {
        ids: bb,
        status: 2,
      };
      userfrozenBatch(data).then((res) => {
        if (res.code == 200) {
          _self.searchFn();
        }
      });
    },
    //批量删除
    userseleteBatchFn() {
      let _self = this;
      let arrid = [];
      for (let item of _self.multipleSelection) {
        arrid.push(item.id);
      }
      let bb = arrid.toString() + ",";
      console.log(bb);
      let data = {
        ids: bb,
      };
      userdeleteBatch(data).then((res) => {
        console.log(res);
        if (res.code == 200) {
          _self.searchFn();
          _self.$refs["trash"].trashTabledataSearch();
        }
      });
    },
    //打开回收站
    trashVisibleFn() {
      let _self = this;
      console.log(_self.$refs["trash"]);
      _self.$refs["trash"].open();
    },
    //是否冻结
    freezingFn(operationData) {
      let _self = this;
      let { status, id } = operationData;
      let data = {
        ids: id,
        status: status == 1 ? 2 : 1,
      };
      userfrozenBatch(data).then((res) => {
        console.log(res);
        if (res.code == 200) {
          _self.searchFn();
        }
      });
    },
    //关闭密码弹框
    cancelpwdFn() {
      let _self = this;
      _self.repwdVisible = false;
      this.$refs.ruleForm.resetFields();
    },
    //提交密码
    changepwdFn(formName) {
      let _self = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            confirmpassword: _self.ruleForm.confirmpassword,
            password: _self.ruleForm.password,
            username: _self.ruleForm.username,
          };
          changePassword(data).then((res) => {
            console.log(res);
            if (res.code == 200) {
              _self.repwdVisible = false;
              return this.$message(res.message);
            }
          });
        }
      });
    },
    //打开重新输入密码的窗口
    openpwdFn(operationData) {
      let _self = this;
      _self.repwdVisible = true;
      _self.ruleForm = operationData;
    },
    //详情
    readmoreFn(operationData) {
      let _self = this;
      _self.isedit = true;
      _self.isreadmore = true;
      _self.dialog = true;
      _self.ruleForm = JSON.parse(JSON.stringify(operationData));
    },
    //添加用户
    adduser() {
      let _self = this;
      _self.ruleForm = {
        id: "",
        username: "",
        password: "",
        confirmpassword: "",
        realname: "",
        workNo: "",
        orgCodeTxt: "",
        post_dictText: "",
        selectedroles: [],
        tenantIdentity: [],
        relTenantIds: "",
        avatar: null,
        birthday: "",
        sex: "",
        email: "",
        phone: "",
        telephone: "",
        activitiSync: "",
      };
      _self.dialog = true;
      _self.isedit = false;
      _self.isreadmore = false;
    },
    //btns方法
    del(id) {
      let _self = this;
      _self.checkList.pop();
      _self.templateSelection = "";
      // _self.tableData1=[]
    },
    // derive导出
    derive() {
      // 导出excel
      let _self = this;
      let data = {
        // selections: _self.selections,
        pageNo: 1,
        pageSize: 10,
      };
      userexportXls(data).then((res) => {
        console.log(res);
        if (typeof window.navigator.msSaveBlob !== "undefined") {
          window.navigator.msSaveBlob(
            new Blob([res], { type: "application/vnd.ms-excel" }),
            "导出文件.xls"
          );
        } else {
          let url = window.URL.createObjectURL(
            new Blob([res], { type: "application/vnd.ms-excel" })
          );
          let link = document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", "导出文件.xls");
          document.body.appendChild(link);
          console.log(link);
          link.click();
          document.body.removeChild(link); //下载完成移除元素
          window.URL.revokeObjectURL(url); //释放掉blob对象
        }
      });
    },
    //树形列表
    getDepartMentMessage() {
      let _self = this;
      getDepartMent().then((res) => {
        // console.log(res.result);
        let depart = res.result;
        _self.selectData = depart;
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          this.$refs.ruleForm.resetFields();
          done();
        })
        .catch((_) => {});
    },
    singleElection(row) {
      let _self = this;
      this.templateSelection = row.id;
      this.checkList = this.tableData2.filter((item) => item.id === row.id);
      _self.tableData1 = this.checkList;
    },
    //选择职务chooseorgCode
    chooseorgCode() {
      let _self = this;
      sysDepartRolelist().then((res) => {
        // console.log(res);
        if (res.code == 0) {
          _self.tableData2 = res.result.records;
        }
      });
    },
    //选择角色分配
    rolequeryallFn() {
      let _self = this;
      rolequeryall().then((res) => {
        console.log(res);
        // alert(1);
        if (res.code == 0) {
          _self.options = res.result;
          console.log(_self.options);
        }
      });
    },

    // //上传头像
    // handleAvatarSuccess(res, file) {
    //   this.imageUrl = URL.createObjectURL(file.raw);
    // },
    // beforeAvatarUpload(file) {
    //   const isJPG = file.type === "image/jpeg";
    //   const isLt2M = file.size / 1024 / 1024 < 2;
    //   if (!isJPG) {
    //     this.$message.error("上传头像图片只能是 JPG 格式!");
    //   }
    //   if (!isLt2M) {
    //     this.$message.error("上传头像图片大小不能超过 2MB!");
    //   }
    //   return isJPG && isLt2M;
    // },
    httprequest(par) {
      let _self = this; // par.flie当前图片对象 // 图片对象上传的话 必须以form表单进行上传
      let formatDate = new FormData();
      formatDate.append("biz", "temp");
      formatDate.append("file", par.file);
      userImg(formatDate).then((res) => {
        console.log(res);
        if (res.code == 0) {
          _self.imageUrl = `http://localhost:8080/jeecg-boot/sys/common/static/${res.message}`;
          _self.ruleForm.avatar = _self.imageUrl;
          // console.log(_self.formInline.avatar);
        }
      });
    },
    //编辑出弹窗
    changeFn(operationData) {
      let _self = this;
      // console.log(_self.readmore);
      _self.isreadmore = false;
      _self.dialog = true;
      _self.isedit = true;
      _self.ruleForm = JSON.parse(JSON.stringify(operationData));
      console.log(operationData);
      console.log(_self.ruleForm);
    },
    submitForm(formName) {
      let _self = this;
      this.$refs[formName].validate((valid) => {
        console.log(valid);
        if (valid) {
          console.log(_self.isedit);
          if (!_self.isedit) {
            let data = {
              username: _self.ruleForm.username,
              password: _self.ruleForm.password,
              confirmpassword: _self.ruleForm.confirmpassword,
              realname: _self.ruleForm.realname,
              workNo: _self.ruleForm.workNo,
              // userIdentity: _self.ruleForm.relTenantIds,
              telephone: _self.ruleForm.telephone
                ? _self.ruleForm.telephone
                : null,
              sex: _self.ruleForm.sex,
              selectedroles: _self.ruleForm.selectedroles
                ? _self.ruleForm.selectedroles.join()
                : "",
              id: _self.ruleForm.id,
              email: _self.ruleForm.email ? _self.ruleForm.email : null,
              birthday: _self.ruleForm.birthday,
              avatar: _self.ruleForm.avatar ? _self.ruleForm.avatar : null,
              // departIds: _self.ruleForm.workNo,
              activitiSync: _self.ruleForm.activitiSync,
              selecteddeparts: _self.ruleForm.orgCodeTxt
                ? _self.brr.join()
                : "",
              relTenantIds: _self.ruleForm.tenantIdentity
                ? _self.ruleForm.tenantIdentity.join()
                : "",
              post: _self.ruleForm.orgCode,
              phone: _self.ruleForm.phone ? _self.ruleForm.phone : null,
            };
            useradd(data).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.loading = false;
                this.dialog = false;
                console.log(1);
                // this.$refs.ruleForm.resetFields();
                _self.searchFn();
              } else {
                return this.$message(res.message);
              }
            });
          } else {
            let data = {
              // username: _self.ruleForm.username,
              // password: _self.ruleForm.password,
              // confirmpassword: _self.ruleForm.confirmpassword,
              // realname: _self.ruleForm.realname,
              // workNo: _self.ruleForm.workNo,
              // // userIdentity: _self.ruleForm.relTenantIds,
              // telephone: _self.ruleForm.telephone,
              // phone: _self.ruleForm.phone,
              // sex: _self.ruleForm.sex,
              // selectedroles: _self.ruleForm.selectedroles,
              // id: _self.ruleForm.id,
              // email: _self.ruleForm.email,
              // birthday: _self.ruleForm.birthday,
              // avatar: _self.ruleForm.avatar ? _self.ruleForm.avatar : null,
              username: _self.ruleForm.username,
              password: _self.ruleForm.password,
              confirmpassword: _self.ruleForm.confirmpassword,
              realname: _self.ruleForm.realname,
              workNo: _self.ruleForm.workNo,
              userIdentity: _self.ruleForm.relTenantIds == "1" ? "1" : "2",
              telephone: _self.ruleForm.telephone
                ? _self.ruleForm.telephone
                : null,
              sex: _self.ruleForm.sex,
              selectedroles: _self.ruleForm.selectedroles
                ? _self.ruleForm.selectedroles.join()
                : "",
              id: _self.ruleForm.id,
              email: _self.ruleForm.email ? _self.ruleForm.email : null,
              birthday: _self.ruleForm.birthday,
              avatar: _self.ruleForm.avatar ? _self.ruleForm.avatar : null,
              // departIds: _self.ruleForm.workNo,
              activitiSync: _self.ruleForm.activitiSync == 1 ? 1 : "0",
              selecteddeparts: _self.ruleForm.orgCodeTxt
                ? _self.brr.join()
                : "",
              relTenantIds: _self.ruleForm.tenantIdentity
                ? _self.ruleForm.tenantIdentity.join()
                : "",
              post: _self.ruleForm.orgCode,
              phone: _self.ruleForm.phone ? _self.ruleForm.phone : null,
            };
            useredit(data).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.loading = false;
                this.dialog = false;
                console.log(_self.ruleForm.relTenantIds);
                // this.$refs.ruleForm.resetFields();
                _self.searchFn();
              } else {
                return this.$message(res.message);
              }
            });
          }
        }
      });
    },
    resetForm(formName) {
      // this.$refs[formName].resetFields();
      let _self = this;
      this.loading = false;
      this.dialog = false;
      this.$refs.ruleForm.resetFields();
    },
    //btns方法

    //删除用户
    userDelFn(val) {
      let _self = this;
      console.log(val);
      let data = {
        id: val.id,
      };
      userdelete(data).then((res) => {
        console.log(res);
        if (res.code == 200) {
          _self.searchFn();
          _self.$refs["trash"].trashTabledataSearch();
        }
      });
    },
    CurrentChange(val) {
      let _self = this;
      _self.currentpage = val;
      _self.searchFn();
    },
    SizeChange(val) {
      let _self = this;
      _self.pagesize = val;
      _self.searchFn();
    },
    SelectionChange(val) {
      let _self = this;
      console.log(val);
      _self.multipleSelection = val;
      // _self.userList();
    },
    scalingFn() {
      let _self = this;
      _self.isScaling = !_self.isScaling;
    },
    async searchFn() {
      let _self = this;
      _self.issearch = true;
      let data = {
        username: _self.formInline.username.trim(),
        sex: _self.formInline.sex,
        realname: _self.formInline.realname.trim(),
        phone: _self.formInline.phone.trim(),
        status: _self.formInline.status,
        pageSize: _self.pagesize,
        pageNo: _self.currentpage,
      };
      await searchlist(data).then((res) => {
        // console.log(res);
        let { records } = res.result;
        if (_self.issearch) {
          if (res.code == 0) {
            let { current, size, total } = res.result;
            (_self.total = total),
              (_self.pagesize = size),
              (current = 1),
              (_self.currentpage = current);
            // console.log(records);
            _self.tableData = records;
          }
        } else {
          if (res.code == 0) {
            let { current, size, total } = res.result;
            (_self.total = total),
              (_self.pagesize = size),
              (_self.currentpage = current);
            // console.log(records);
            _self.tableData = records;
          }
        }
      });
    },
    resetFn() {
      let _self = this;
      _self.formInline = {
        username: "",
        sex: "",
        realname: "",
        phone: "",
        status: "",
      };
      _self.searchFn();
    },
  },
};
</script>

<style>
.headerimg {
  height: 40px;
  width: 40px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
</style>